<?php
namespace App\Services\IntegralGoods\Goods;

use App\Models\IntegralGoods\Goods;
use App\Services\Manage\Api;
/**
 * 商品 逻辑层
 * Class GoodsLogic
 * @package App\Services\Goods\Goods
 */
class GoodsLogic
{
    // 添加新的广告位
    /*内部根据id进行简单直接的db查询商品列表*/
    public static function dbGoodsList($data)
    {
        return Goods::whereIn('id',$data['id'])
            ->where('is_del', '<>', 2)
            ->get()
            ->toArray();
    }

    /*艺货后台发布积分商品*/
    public function addIntegralGoods($data): array
    {
        $data['number'] = $data['origin_number'];
        $data['status'] = 1;
        $imgUrls = explode(",", $data['imgs_urls']);
        !$data['profile_pic'] && $data['profile_pic'] = $imgUrls[0];
        $data['create_time'] = time();
        $data['update_time'] = time();
        try{
            unset($data['token']);
            unset($data['app_id']);
            $res =(new Goods())->fillable(['user_id', 'cate_id', 'title', 'integral', 'product_code', 'profile_pic', 'imgs_urls', 'imgs_ids', 'origin_number', 'order', 'desc', 'status', 'number'])->insert($data);
        }catch(\Exception $e){
            return server_err(null, $e->getMessage());
        }
        return auto_return($res);
    }

    /*艺货后台修改积分商品*/
    public function updateIntegralGoods($data): array
    {
        $_where['id'] = $data['id'];
        $_where['is_del'] = 0;
        $info = Goods::where($_where)->first();
        $number = isset($data['number']) ? $data['number'] : $info['number'];
        $origin_number = isset($data['origin_number']) ? $data['origin_number'] : $info['origin_number'];
        if($number > $origin_number){
            return verify_err(null, '剩余数量不能大于原始数量');
        }
        unset($data['token']);
        unset($data['app_id']);
        $res = (new Goods())->fillable(['cate_id', 'user_id', 'title', 'integral', 'profile_pic', 'imgs_urls', 'imgs_ids', 'number', 'origin_number', 'order', 'desc'])->where($_where)->update($data);
        return auto_return($res, '成功修改该商品', '修改商品失败');
    }

    /*后台获取积分商品列表数据*/
    public function allGoodsList($data): array
    {
        $_where['is_del'] = 0;
       // $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
        $manageAppId = Api::getAppId();
        $query = Goods::leftJoin('goods_category', 'goods_category.id', '=', 'integral_goods.cate_id')
            ->select('integral_goods.*','goods_category.name as cate_name')
            ->where($_where);
            if(!empty($data['keywords'])){
                $query->where('goods.title', 'like', '%' . $data['keywords'] . '%');
            }
            if($manageAppId != $data['app_id']){
                $app_id = $data['app_id'];
                $query->where(function ($query) use ($app_id){
                     $query->where('goods_category.app_id',$app_id)->orWhere(function ($query) use ($app_id){
                          $query->whereRaw("FIND_IN_SET($app_id,`other_app_read`)");
                     })->orWhere('goods_category.is_public', 1);
                });
            }
        $list = $query->orderBy('integral_goods.id', 'desc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();
        return auto_return_page($list);
    }

    /*管理员批量操作（审核、下架）商品*/
    public function batchOperaGoods($data, $operate): array
    {
        //左链接分类表，对app_id进行验证操作
        $manageAppId = Api::getAppId();
       // $_where['integral_goods.id'] = ['in',$data['id']];
        $_where['integral_goods.is_del'] = 0;


        $saveData['integral_goods.status'] = $data['status'];
        $query = Goods::leftJoin('goods_category', 'goods_category.id', '=', 'integral_goods.cate_id')
            ->where($_where)
        ->whereIn('integral_goods.id',$data['id']);
        if($operate == 'manageBatchSoldOutGoods'){
            $query->whereIn('integral_goods.status',['1','4']);
        }
        if($manageAppId != $data['app_id']){
            $app_id = $data['app_id'];
            $query->where(function ($query) use ($app_id){
                $query->where('goods_category.app_id',$app_id)->orWhere(function ($query) use ($app_id){
                    $query->whereRaw("FIND_IN_SET($app_id,`other_app_read`)");
                })->orWhere('goods_category.is_public', 1);
            });
        }
       $res = $query->update($saveData);
        return auto_return($res);
    }

    /*管理员删除一个或多个商品*/
    public function manageDelGoods($data): array
    {

        $manageAppId = Api::getAppId();
        !is_array($data) && $data['id'] = [$data['id']];
        $_where['integral_goods.is_del'] = 0;
        $saveData['integral_goods.is_del'] = 1;
        //左链接分类表，对app_id进行验证操作
        $query = Goods::leftJoin('goods_category', 'goods_category.id', '=', 'integral_goods.cate_id')
            ->where($_where);
            if(is_array($data['id'])) {
                $query->whereIn('integral_goods.id', $data['id']);
            }else{
                $query->where('integral_goods.id', $data['id']);
            }
        if($manageAppId != $data['app_id']){
            $app_id = $data['app_id'];
            $query->where(function ($query) use ($app_id){
                $query->where('goods_category.app_id',$app_id)->orWhere(function ($query) use ($app_id){
                    $query->whereRaw("FIND_IN_SET($app_id,`other_app_read`)");
                })->orWhere('goods_category.is_public', 1);
            });
        }
        $res = $query->update($saveData);
        return auto_return($res);
    }
//    /*前台获取积分商品分页列表*/
//    public function frontGoodsList($data): array
//    {
//        $_where['goods.is_del'] = 0;
//        $_where['goods.status'] = 1;
//        $_where['goods.number'] = ['gt', 0];
//        $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
//        $manageAppId = ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
//        $list = $this->model($this->goodsModel)
//            ->alias('goods')
//            ->field('goods.*,goods_category.name as cate_name')
//            ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
//            ->where($_where)
//            ->where($_whereOr)
//            ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
//            ->toArray();
//        return auto_return_page($list);
//    }
//
//    /*前台获取某积分商品的详情*/
//    public function frontGoodsInfo($data): array
//    {
//        $_where['goods.number'] = ['gt', 0];
//        $_where['goods.status'] = 1;
//        $_where['goods.is_del'] = 0;
//        $_where['goods.id'] = $data['id'];
//        $manageAppId = ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
//        $info = $this->model($this->goodsModel)
//            ->alias('goods')
//            ->field('goods.*,goods_category.name as cate_name')
//            ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
//            ->where($_where)
//            ->where($_whereOr)
//            ->find();
//        return auto_return($info);
//    }

   
}